<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\Controller;
use App\Http\Requests\customer\TrajectoryRequest;
use App\Models\customer\TrajectoryModel;
use App\Models\customer\TrajectoryNodeModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Trajectory extends Controller
{
    use TrajectoryRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $items = TrajectoryModel::query()->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'platform' => $item->customer_id,
                'customer_id' => $item->customer_id,
                'platform_name' => $item->customer ? $item->customer->name : '',
                'warn_times' => 0,
                'updated_at' => $item->updated_at ?? '',
                'status' => $item->status,
                'status_text' => TrajectoryModel::STATUS[$item->status] ?? '',
            ];
            return $res;
        });

        return $this->success(['list' => $list]);
    }


    /**
     * Notes:获取节点列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request) {
        $id = intval($request->input('id', 0));

        $items = TrajectoryNodeModel::query()->with('node')->where('trajectory_id', $id)->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'node_id' => $item->node_id,
                'node_name' => $item->node ? $item->node->name_cn : '',
                'time' => $item->time,
                'unit' => $item->unit,
                'unit_text' => TrajectoryNodeModel::UNIT[$item->unit] ?? '',
            ];
            return $res;
        });

        return $this->success(['list' => $list]);
    }


    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $trajectory = TrajectoryModel::find($params['id']);
        if(!$trajectory){
            return $this->error('数据不存在');
        }

        DB::beginTransaction();
        try {
            $model = new TrajectoryNodeModel();
            $model->trajectory_id = $params['id'];
            $model->node_id = $params['node_id'];
            $model->time = $params['time'];
            $model->unit = $params['unit'];
            $model->created_admin_id = $auth->id;
            $model->save();

            $trajectory->updated_at = date('Y-m-d H:i:s');
            $trajectory->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = TrajectoryNodeModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        DB::beginTransaction();
        try {
            $model->node_id = $params['node_id'];
            $model->time = $params['time'];
            $model->unit = $params['unit'];
            $model->save();

            $trajectory = TrajectoryModel::find($model->trajectory_id);
            $trajectory->updated_at = date('Y-m-d H:i:s');
            $trajectory->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();
    }


    public function delete(int $id){
        $model = TrajectoryNodeModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

}
